﻿learn_language = {
	
	skill = learning
	desc = learn_language_desc_general
	cooldown = { months = 2 }
	uses_agents = no
	power_per_skill_point = 1
	spymaster_power_per_skill_point = 0	
	resistance_per_skill_point = 0
	spymaster_resistance_per_skill_point = 0
	uses_resistance = no
	minimum_progress_chance = 5
	maximum_progress_chance = 95
	minimum_success = 5
	maximum_success = 95
	icon = icon_scheme_culture
	
	allow = {
		age >= 6
		NOR = {
			culture = { # You can't learn your own native language
				has_same_culture_language = scope:target.culture
			}
			knows_language_of_culture = scope:target.culture
			custom_description = { # Should not be able to target the same language twice
				object = scope:target
				text = scheme_target_same_language_as_existing_scheme
				any_scheme = {
					scheme_type = learn_language
					scheme_target = {
						culture = { # You can't learn the same language with two different schemes
							has_same_culture_language = scope:target.culture
						}
					}
				}
			}
		}

		scope:target = {
			age >= 10
			#is_imprisoned = no # TODO this seems unfair to end the scheme for?
		}
		is_imprisoned = no

		#AI blockers
		trigger_if = {
			limit = { is_ai = yes }

			scope:target = {
				trigger_if = { #So players don't get spammed by AI's
					limit = {
						is_ai = no
					}
					NOT = {
						any_targeting_scheme = {
							scheme_type = learn_language
						}
					}
				}
			}
		}
	}
	
	valid = {
		NOR = {
			culture = { # You can't learn your own native language
				has_same_culture_language = scope:target.culture
			}
			knows_language_of_culture = scope:target.culture
		}
		scope:target = {
			OR = {
				exists = location
				in_diplomatic_range = scope:owner
			}
		}
	}

	base_success_chance = {
		base = 0
		
		modifier = {
			add = {
				value = scope:owner.learning
				multiply = 3
			}
			desc = SCHEME_LEARNING_MODIFIER
			scope:owner.learning > 0
		}
		
		modifier = {
			add = {
				value = scope:owner.piety_level
				multiply = 10
			}
			desc = SCHEME_PIETY_LEVEL_MODIFIER
			scope:owner.piety_level > 0
			scope:owner.culture = {
				has_cultural_parameter = language_success_chance_per_piety_level
			}
		}
		
		modifier = {
			add = scope:owner.cp:councillor_court_chaplain.learning
			desc = SCHEME_CHAPLAIN_HELP_LEARNING_MODIFIER
			exists = scope:owner.cp:councillor_court_chaplain
			scope:owner.cp:councillor_court_chaplain.learning > 0
		}
		
		modifier = {
			add = {
				value = 5
				multiply = {
					value = scope:owner.num_of_known_languages
					subtract = 1
				}
			}
			desc = SCHEME_KNOWN_LANGUAGES_MODIFIER
			scope:owner.num_of_known_languages > 1
		}
		
		modifier = {
			add = 10
			scope:owner.culture = { has_same_language_group_as = { TARGET = scope:target.culture } }
			desc = SCHEME_SAME_LANGUAGE_GROUP_AS_NATIVE_LANGUAGE
		}
		modifier = {
			add = 5
			NOT = { scope:owner.culture = { has_same_language_group_as = { TARGET = scope:target.culture } } }
			scope:owner.culture = { has_same_language_family_as = { TARGET = scope:target.culture } }
			desc = SCHEME_SAME_LANGUAGE_FAMILY_AS_NATIVE_LANGUAGE
		}
		modifier = {
			add = 5
			scope:owner.culture = { is_in_language_union_with = { TARGET = scope:target.culture } }
			desc = SCHEME_SAME_LANGUAGE_UNION_AS_NATIVE_LANGUAGE
		}
		
		modifier = {
			add = {
				value = 30
				subtract = scope:owner.age
				multiply = 2
			}
			desc = SCHEME_YOUTH_MODIFIER
			scope:owner.age < 30
		}
		
		modifier = {
			add = 25
			desc = SCHEME_HERITAGE_MODIFIER
			scope:target.culture = {
				has_same_culture_heritage = scope:owner.culture
			}
		}
		
		modifier = {
			add = 50
			desc = SCHEME_LOYAL_SOLDIERS_MODIFIER
			scope:owner = {
				culture = {
					has_cultural_parameter = extra_learn_language_success_chance_vs_liege
				}
				any_liege_or_above = {
					this = scope:target
				}
			}
		}
		
		modifier = {
			add = 30
			desc = SCHEME_CULTURE_BLENDING_MODIFIER
			scope:owner = {
				culture = {
					has_cultural_parameter = learns_more_languages
				}
			}
		}

		#Intellect good
		modifier = {
			desc = "scheme_intellect_good_1"
			scope:owner = { has_trait = intellect_good_1 }
			add = 10
		}
		
		modifier = {
			desc = "scheme_intellect_good_2"
			scope:owner = { has_trait = intellect_good_2 }
			add = 20
		}
		
		modifier = {
			desc = "scheme_intellect_good_3"
			scope:owner = { has_trait = intellect_good_3 }
			add = 30
		}
		
		modifier = {
			scope:target = { has_trait = stubborn }
			desc = "SCHEME_TRAIT_STUBBORN"
			add = 10
		}
		
		modifier = {
			scope:target = { has_trait = ambitious }
			desc = "SCHEME_TRAIT_AMBITIOUS"
			add = 5
		}
		
		#Paranoid value is added automatically because of trait	parameter enemy_personal_scheme_success_chance_add 
		
		# Lifestyles

		# Dynasty Kin Personal Scheme Success Chance on Dynasty Perk
		modifier = {
			add = kin_legacy_4_success_chance
			desc = KIN_LEGACY_DESC
			exists = scope:owner.dynasty
			scope:owner.dynasty = {
				has_dynasty_perk = kin_legacy_4
			}
			scope:target.dynasty = scope:owner.dynasty
		}

		# House Personal Scheme Success Chance on Cultural Parameter
		modifier = {
			add = cultural_house_personal_scheme_success_chance
			desc = KIN_PARAMETER_DESC
			exists = scope:owner.house
			exists = scope:target.house
			scope:owner.culture = {
				has_cultural_parameter = cultural_house_personal_scheme_success_chance
			}
			scope:target.house = scope:owner.house
		}

		# Pedagogy Perk
		modifier = {
			add = 25
			desc = LEARN_LANGUAGE_PEDAGOGY_PERK_DESC
			scope:owner = {
				has_perk = pedagogy_perk
			}
		}

		# Thicker Than Water Perk
		modifier = {
			add = thicker_than_water_bonus
			desc = BEFRIEND_THICKER_THAN_WATER_PERK_DESC
			scope:owner = {
				has_perk = thicker_than_water_perk
			}
			scope:target = {
				is_close_or_extended_family_of = scope:owner
			}
		}
	}

	on_ready = {
		random = {
			chance = scope:scheme.scheme_success_chance

			save_scope_value_as = {
				name = scheme_successful
				value = yes
			}
		}
		scope:owner = {
			if = {
				limit = {
					exists = scope:scheme_successful
				}
				trigger_event = {
					on_action = learn_language_success
				}
			}
			else = {
				trigger_event = {
					on_action = learn_language_failure
				}	
			}
		}
	}

	on_invalidated = {
		scheme_target = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}
		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				send_interface_toast = {
					title = learn_language_invalidated
					custom_tooltip = learn_language_invalidated_dead_desc
					left_icon = scope:target
				}
			}
		}
		else_if = {
			limit = {
				scope:owner = {
					#OR = {
						culture = { # You can't learn your own native language
							has_same_culture_language = scope:target.culture
						}
						#knows_language_of_culture = scope:target.culture
					#}
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = learn_language_invalidated
					custom_tooltip = learn_language_invalidated_cannot_learn_language_desc
					left_icon = scope:target
				}
			}
		}

		if = {
			limit = {
				scope:target = { 
					NOT = { in_diplomatic_range = scope:owner }
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = learn_language_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_not_in_diplomatic_range
					}
				}
			}
		}
		
		scope:owner = {
			if = {
				limit = {
					is_alive = yes
					scope:target = {
						OR = {
							is_alive = no
							NOT = { in_diplomatic_range = scope:owner }
						}
					}
					NOR = {
						culture = { # You can't learn your own native language
							has_same_culture_language = scope:target.culture
						}
						knows_language_of_culture = scope:target.culture
					}
				}
				set_variable = {
					name = language_progress
					value = prev.scheme_progress
				}
				trigger_event = learn_language_outcome.3000
			}
		}
	}

	on_monthly = {
		save_scope_as = scheme
		scheme_target = { save_scope_as = target }
		scheme_owner = {
			save_scope_as = owner
			trigger_event = { on_action = learn_language_ongoing }
		}
	}
	success_desc = "LEARN_LANGUAGE_SUCCESS_DESC"
}
